package com.tan.kotlintest.fragment

import android.support.v7.widget.LinearLayoutManager
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.tan.kotlintest.R
import com.tan.kotlintest.adapter.GirlRecyclerAdapter
import com.tan.kotlintest.base.BaseFragment
import com.tan.kotlintest.base.BaseResponse
import com.tan.kotlintest.bean.GirlListBean
import com.tan.kotlintest.bean.PageBean
import com.tan.kotlintest.constant.ChatApi
import com.tan.kotlintest.net.AjaxCallback
import com.tan.kotlintest.net.NetCode
import com.tan.kotlintest.util.LogUtil
import com.tan.kotlintest.util.ToastUtil
import com.zhy.http.okhttp.OkHttpUtils
import kotlinx.android.synthetic.main.fragment_girl_layout.*
import java.util.*

/*
 * Copyright (C) 2018 重庆一聊高科技有限公司
 * 版权所有
 *
 * 功能描述:  女神Fragment
 * 作者：txj
 * 创建时间：2018/9/18
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
class GirlFragment : BaseFragment() {

    private var mAdapter: GirlRecyclerAdapter? = null
    private var mCurrentPage = 1
    private val mGirlListBeans = ArrayList<GirlListBean>()

    override fun initLayout(): Int {
        return R.layout.fragment_girl_layout
    }

    override fun initView() {
        refreshLayout.setOnRefreshListener {
            getGirlList(refreshLayout, true, 1)
        }
        refreshLayout.setOnLoadMoreListener {
            getGirlList(refreshLayout, false, mCurrentPage + 1)
        }

        val gridLayoutManager = LinearLayoutManager(context)
        content_rv.layoutManager = gridLayoutManager
        mAdapter = GirlRecyclerAdapter(mContext)
        content_rv.adapter = mAdapter
        //view自带的方法,判断recyclerview是否滑动到顶部底部
        content_rv.canScrollVertically(1)
        content_rv.canScrollHorizontally(1)
    }

    override fun onFirstVisible() {
        LogUtil.i("GirlFragment第一次可以见")
        getGirlList(refreshLayout, true, 1)
    }

    /**
     * 获取女神列表
     */
    private fun getGirlList(refreshLayout: SmartRefreshLayout, isRefresh: Boolean, page: Int) {
        OkHttpUtils.post().url(ChatApi.GET_HOME_PAGE_LIST)
                .addParams("userId", "324")
                .addParams("page", page.toString())
                .addParams("queryType", "0")
                .build().execute(object : AjaxCallback<BaseResponse<PageBean<GirlListBean>>>() {

                    override fun onResponse(response: BaseResponse<PageBean<GirlListBean>>?, id: Int) {
                        if (response != null && response.m_istatus == NetCode.SUCCESS) {
                            val pageBean = response.m_object
                            if (pageBean != null) {
                                val girlListBeans = pageBean.data
                                if (girlListBeans != null) {
                                    val size = girlListBeans.size
                                    if (isRefresh) {
                                        mCurrentPage = 1
                                        mGirlListBeans.clear()
                                        mGirlListBeans.addAll(girlListBeans)
                                        mAdapter?.loadData(mGirlListBeans)
                                        refreshLayout.finishRefresh()
                                        if (size >= 10) {//如果是刷新,且返回的数据大于等于10条,就可以load more
                                            refreshLayout.isEnableLoadMore = true
                                        }
                                    } else {
                                        mCurrentPage++
                                        mGirlListBeans.addAll(girlListBeans)
                                        mAdapter?.loadData(mGirlListBeans)
                                        if (size >= 10) {
                                            refreshLayout.finishLoadMore()
                                        }
                                    }
                                    if (size < 10) {//如果数据返回少于10了,那么说明就没数据了
                                        refreshLayout.finishLoadMoreWithNoMoreData()
                                    }
                                }
                            }
                        } else {
                            ToastUtil.showToast(activity, R.string.system_error)
                            if (isRefresh) {
                                refreshLayout.finishRefresh()
                            } else {
                                refreshLayout.finishLoadMore()
                            }
                        }
                    }

                })
    }


}